<?php
/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
defined('_VALID_MOS') or die('Direct Access to this location is not allowed.');

require_once 'Savant2/Plugin.php';

class Savant2_Plugin_leagueman_get_player_adminstat extends Savant2_Plugin
{

    function plugin($player)
    {
        global $database;

        $sql = "SELECT pm. * , tm.teammap_id, l.league_name AS league, s.season_name AS season, t.team_name AS team, p.project_id, p.project_name, p.eventtypes AS eventtypes
                  FROM #__leagueman_playermap pm
            INNER JOIN #__leagueman_project p ON p.project_id = pm.playermap_project
            INNER JOIN #__leagueman_teammap tm ON tm.teammap_team = pm.playermap_team AND tm.teammap_project = pm.playermap_project
             LEFT JOIN #__leagueman_league l ON l.league_id = p.project_league
             LEFT JOIN #__leagueman_season s ON s.season_id = p.project_season
             LEFT JOIN #__leagueman_team t ON t.team_id = pm.playermap_team
                 WHERE pm.playermap_player = $player->player_id
              ORDER BY s.ordering";

        $database->setQuery($sql);
        $rows = $database->loadObjectList();

        $fields = array();
        foreach ($rows AS $row) {
            $fields = array_merge($fields, unserialize($row->eventtypes));
        }

        $fields = array_unique($fields);

        $eventtypemap = LM_Eventtypemap::getInstance();

        ob_start();
?>
        <table width="100%" class="adminlist">
            <tr>
                <th width="20%"><?php echo 'Player'; ?></th>
                <th width="20%"><?php echo 'Project'; ?></th>
                <th width="20%"><?php echo _LM_TEAM; ?></th>

                <?php foreach ($fields AS $field) {
                    $eventtype = $eventtypemap->getEventtype($field); ?>

                    <th width="5%">
                        <div align="center"><?php echo $eventtype->getIconImage(); ?></div>
                    </th>
                <?php } ?>

            </tr>
    <?php
        $k = 1;
        foreach ($rows as $row) {

            $projectLink = sprintf("index2.php?option=com_leagueman&act=projects&task=edit&id=%s", $row->project_id);
            $teamLink    = sprintf("index2.php?option=com_leagueman&act=teammap&task=open&id=%s", $row->teammap_id);
            $playerLink  = sprintf("index2.php?option=com_leagueman&act=playermap&task=edit&id=%s", $row->playermap_id);

            $class = ($k == 1) ? "sectiontableentry1" : "sectiontableentry2";
    ?>
            <tr class="<?php echo $class ?>">
                <td><a href="<?php echo $playerLink; ?>"><?php echo $player->getName(); ?></a></td>
                <td><a href="<?php echo $projectLink; ?>"><?php echo $row->project_name; ?></a></td>
                <td><a href="<?php echo $teamLink; ?>"><?php echo $row->team; ?></a></td>

        <?php
            foreach ($fields AS $field) {
                $eventtype = $eventtypemap->getEventtype($field);
                $association = $eventtype->playermap_association;
        ?>

                <td><div align="center"><?php echo $row->$association; ?></div></td>
<?php } ?>

        </tr>
    <?php
            $k = 1 - $k;
        }
    ?>
    </table>

<?php
        $html = ob_get_contents();
        ob_end_clean();

        return $html;
    }

}